package com.gm.wholesale.module.settlement.ui.main;

import android.util.ArrayMap;
import android.widget.BaseAdapter;

import com.gm.commonlib.base.baseAdapter.CommonAdapter;
import com.gm.commonlib.base.baseAdapter.ViewHolder;
import com.gm.commonlib.base.baseBean.BasePageData;
import com.gm.commonlib.base.baseui.baseListviewUI.BaseListFragment;
import com.gm.commonlib.module.evenbus.EventBusUtils;
import com.gm.commonlib.module.evenbus.EventMessage;
import com.gm.commonlib.network.HttpServicesHolder;
import com.gm.commonlib.util.numberUtils.NumberUtils;
import com.gm.wholesale.R;
import com.gm.wholesale.module.evenbus.EvenBusUtil;
import com.gm.wholesale.module.evenbus.EventBusType;
import com.gm.wholesale.module.settlement.api.IReconciliationApi;
import com.gm.wholesale.module.settlement.bean.custorm.CustormReviceBean;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;

/**
 * 左边客户列表界面
 */
public class AccountCustormListFragment extends BaseListFragment<CustormReviceBean, BasePageData<CustormReviceBean>> {
    private int mCurrentPos;

    private boolean mIsFirstEnter=true;

    @Override
    public void initPullToRefreshListView() {
        super.initPullToRefreshListView();
        setBodybg(getResources().getColor(R.color.white));
        setRefreshAndLoadEnable(true, false);
        pullToRefreshListView.setDivider(getResources().getDrawable(R.drawable.list_driver_shap));

    }

    @Override
    public void onRefreshView() {
        mCurrentPos = 0;
        mIsFirstEnter=true;
        super.onRefreshView();
    }

    @Override
    protected void getDataFinish(List<CustormReviceBean> mDatas) {
        if (mDatas != null && !mDatas.isEmpty()) {
            if (mCurrentPos > 0) {
                mDatas.get(mCurrentPos).setCheck(true);
            } else {
                mDatas.get(0).setCheck(true);
                if (mIsFirstEnter){
                    showCustromSettlementUI(mDatas.get(0));
                    mIsFirstEnter=false;
                }
            }
        }
    }

    @Override
    protected BaseAdapter createTempBaseAdapter() {

        return new CommonAdapter<>(getActivity(), mDatas, R.layout.list_account_custorm_item) {
            @Override
            public void convert(ViewHolder viewHolder, final CustormReviceBean data) {
                data.setPos(viewHolder.getPosition());
                viewHolder.setText(R.id.custormTv, data.getCustomerName());
                viewHolder.setText(R.id.needPayMoneyTv, "￥" + NumberUtils.getTowFloatData(data.getShouldPayAmount()));
                if (data.isCheck()) {
                    viewHolder.getConvertView().setBackgroundColor(mContext.getResources().getColor(R.color.color_e4f4eb));
                } else {
                    viewHolder.getConvertView().setBackgroundColor(mContext.getResources().getColor(R.color.white));
                }
                viewHolder.getConvertView().setOnClickListener(v -> {
                    setDataClick(viewHolder.getPosition());
                    mCurrentPos = viewHolder.getPosition();
                    showCustromSettlementUI(data);
                });

            }
        };

    }

    private void setDataClick(int pos) {
        for (CustormReviceBean data : mDatas) {
            if (pos == data.getPos()) {
                data.setCheck(true);
            } else {
                data.setCheck(false);
            }
        }
        notifyListDataSetChanged();

    }


    private void showCustromSettlementUI(CustormReviceBean data) {
        EvenBusUtil.showCustormSettlementDetail(data);
    }

    @Override
    public void onReceiveEvent(EventMessage event) {
        if (event.getCode() == EventBusType.UPDATE_CUSTORM_LIST.getType()) {
            requestPageData(1);
        }
    }


    @Override
    protected HttpServicesHolder<?, BasePageData<CustormReviceBean>> getHttpServicesHolder(long index) {
        return new HttpServicesHolder<IReconciliationApi, BasePageData<CustormReviceBean>>() {
            @Override
            public Observable<BasePageData<CustormReviceBean>> getHttpObservable(IReconciliationApi api) {
                ArrayMap<String, Object> arrayMap = new ArrayMap<>();
                arrayMap.put("page", 1);
                arrayMap.put("pageSize", 100000);//默认获取全部不分页
                return api.getCustormReviceMoney(arrayMap);
            }
        };
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
